var CryptoJS = require('crypto-js');
const JSEncrypt = require('jsencrypt');
const crypt = new JSEncrypt();


function getRandomStr(t) {
    for (var e = ""; e.length < t;)
        e += Math.random().toString(36).substr(2);
    return e = e.slice(0, t)
}


RSA_PUBLIC_KEY = '-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDnOWe/gs033L/2/xR3oi6SLAMPBY5VledUqqH6dbCNOdrGX4xW+1x6NUfvmwpHRBA2C7xWDDvOIldTl0rMtERTDy9homrVqEcW6/TY+dSVFL3e2Yg2sVaehHv7FhmATkgfC2FcXt8Wvm99QpKRSrGKpcFYJwOj2F8hJh+rTG0IPQIDAQAB-----END PUBLIC KEY-----'
crypt.setPublicKey(RSA_PUBLIC_KEY)

function encrypt(t) {
    t = JSON.stringify(t);
    const e = '01moik64z8pfrka2', //getRandomStr(16)
        n = 'yyijus6bt9abnl5f';   //getRandomStr(16)
    return {
        i: CryptoJS.AES.encrypt(t, CryptoJS.enc.Utf8.parse(e), {
            iv: CryptoJS.enc.Utf8.parse(n)
        }).toString(),
        k: crypt.encrypt(e + n)
    }
}



function get_url(e, t, token) {
    var a = encrypt(e);
    var version = 'v1';
    var HOSTS = "https://captcha.yunpian.com";
    var r = 'ofobn3scebs';
    const APP_ID = '5de60e59c04d4dab96bdbc312370ad26';
    t = "".concat(HOSTS, "/").concat(version).concat(t, "?cb=").concat(r, "&i=").concat(encodeURIComponent(a.i), "&k=").concat(encodeURIComponent(a.k))
    if (token) {
        token && (t += "&token=".concat(token))
    }
    t += "&captchaId=".concat(APP_ID)
    return t

}

function get_captcha_url() {
    const e = {
        "browserInfo": [
            {
                "key": "userAgent",
                "value": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36"
            },
            {
                "key": "language",
                "value": "zh-CN"
            },
            {
                "key": "hardware_concurrency",
                "value": 4
            },
            {
                "key": "resolution",
                "value": [
                    1920,
                    1080
                ]
            },
            {
                "key": "navigator_platform",
                "value": "Win32"
            }
        ],
        "nativeInfo": null,
        "additions": {},
        "options": {
            "sdk": "https://gitee.com/static/javascripts/yunpian/riddler-sdk-0.2.2.js?t=1629687362163",
            "sdkBuildVersion": "1.5.0(2021080901)",
            "hosts": "https://captcha.yunpian.com"
        },
        "fp": "936c820be5de49fb1593124400161d39",
        "address": "https://gitee.com",
        "yp_riddler_id": "7054efab-7b80-4858-90be-6d674b909339"
    };
    return get_url(e, '/jsonp/captcha/get')
}

function get_points(offset) {
    let points = [];
    let start_x = 591;
    let start_y = 419;
    let start_time = 95;
    points.push([start_x, start_y, start_time])
    let end_x = start_x + (offset / (480 / 306))
    while (true) {
        start_x += Math.round(Math.random() * 5);
        start_y += Math.round(Math.random() * 2) - 1;
        start_time += Math.round(Math.random() * 3) + 5;

        if (start_x > end_x) {
            break
        } else {
            points.push([start_x, start_y, start_time])
        }
    }
    return points
}


function get_distanceX(offset) {
    return (306 - 59) * ((offset / (480 / 306)) / (306 - 42)) / 306;
}


function get_verify_url(offset, token) {
    let distanceX = get_distanceX(offset);
    let points = get_points(offset);
    let e = {
        'distanceX': distanceX,
        'points': points,
        'address': 'https://gitee.com',
        'fp': '936c820be5de49fb1593124400161d39',
        'yp_riddler_id': '7054efab-7b80-4858-90be-6d674b909339'
    }
    return get_url(e, '/jsonp/captcha/verify', token)
}

console.log(get_captcha_url())


